<?php
namespace XtwsBack\Controller;
use Think\Controller;

class ExchangeGoodsController extends CommonController {
	
    public function index(){
        $exhange = M('exchange_goods');
        $where="";
        if($_GET['keyword']!=''){
            $where=" and title like '%".$_GET['keyword']."%'";
            $this->assign('keyword',$_GET['keyword']);// 赋值分页输出
        }
        $count = $exhange->where('state <> 3 ')->count();// 查询满足要求的总记录数
        $Page = new \Think\Page($count,100);// 实例化分页类 传入总记录数和每页显示的记录数(25)
        $show = $Page->show();// 分页显示输出
        $list = $exhange->where('state <> 3 '.$where)->order('id desc')->limit($Page->firstRow.','.$Page->listRows)->select();
        foreach($list as $k => $v) {// 查询拼团商品信息，拼团的商家或者农户的信息
            $list[$k]['head_img'] = isMorePic($list[$k]['head_img']);
        }//end foreach
        $this->assign('list',$list);// 赋值数据集
        $this->assign('page',$show);// 赋值分页输出
        $this->display();
    }//end fun


    // 添加兑换商品
    public function addGoods(){
        $exhange = M('exchange_goods');
        if(IS_POST){
            $data = $_POST;
            $data['addtime'] = time();
            if($data['c_id']!=''){
                $c_id_str = array_unique ($data['c_id']);   //去重
                $data['c_id_str'] = implode(",",$c_id_str); //打散为字符串
                unset($data['c_id']);
            }
            $res = $exhange -> add($data);
            if($res){
                $this -> success('提交成功','http://www.xiangtws.com/XtwsBack/ExchangeGoods/index');
            }else{
                $this -> error('提交失败');
            }// end if
        }else{
            // 查询省市信息
            $area = D('area');
            $getfirst = $area->where("`re_id` =0")->select();
            //获取单位规格下的单位分类
            $category = M('category');
            $list_category = $category->where('pid = 22 AND status = 1')->field('id,title')->select();
            $this->assign('areali',$getfirst);
            $this->assign('list_category',$list_category);
            $this -> display();
        }// end if
    }// end fun


    // 修改兑换商品
    public function editGoods(){
        $exhange = M('exchange_goods');
        if(IS_POST){
            $data = $_POST;
            $data['uptime'] = time();
            // 处理限制小区id
            if($data['c_id']!=''){
                $c_id_str = array_unique ($data['c_id']);   //去重
                $data['c_id_str'] = implode(",",$c_id_str); //打散为字符串
                unset($data['c_id']);
            }
            $res = $exhange->save($data);
            if($res){
                $this -> success('提交成功','http://www.xiangtws.com/XtwsBack/ExchangeGoods/index');
            }else{
                $this -> error('提交失败');
            }// end if
        }else{
            $id = intval(I('id'));
            $info = $exhange->where("id={$id}")->find();
            //首页图分离
            $info['h_img']=explode(",",$info['head_img']);
            $info['c_img']=explode(",",$info['con_img']);
            // 查询省市信息
            $area = D('area');
            $getfirst = $area->where("`re_id` =0")->select();
            // 查询限制地区的省信息
            $info['area1_id'] = $area->where('id='.$info['area_id'])->getField('re_id');
            $info['area_name'] = $area->where('id='.$info['area_id'])->getField('name');
            $info['area_id_id'] = $area->where('id='.$info['area_id'])->getField('area_id');
            //获取单位规格下的单位分类
            $category = M('category');
            $list_category = $category->where('pid = 22 AND status = 1')->field('id,title')->select();
            // 查询限制小区信息
            $shop_store = M('shop_store');
            if($info['c_id_str']!=''){
                $c_id_str = explode(",",$info['c_id_str']);
                foreach ($c_id_str  as $k=>$v){
                    $store[$k] = $shop_store->where("c_id=".$v)->field('c_id,storename,address')->find();
                }
            }else{
                $store = 1;
            }
            if($info['u_id']!=0){//门店兑换商品
                $store1 = $shop_store->where("u_id=".$info['u_id'])->field('storename,address')->find();
                $this -> assign('store1',$store1);
            }
            $this->assign('areali',$getfirst);
            $this->assign('list_category',$list_category);
            $this -> assign('info',$info);
            $this -> assign('store',$store);
            $this -> display();
        }// end if
    }// end fun

    // 店铺名称搜索
    public function searchStore(){
        $store_key = I('store_key');    //搜索关键词
        if($store_key){
            $where = " status = 1 AND c_id <> 0 AND storename like '%".$store_key."%'";
            $shop_store = M('shop_store');
            $list = $shop_store->where($where)->field('storename,c_id,address,u_id')->select();
            if($list!=''){
                $this->ajaxReturn($list);
            }
        }
    }// end fun

    // 删除商品
    public function del_goods(){
        $id  = intval(I('id'));
        if($id){
            $exhange = M('exchange_goods');
            $res = $exhange->where('id='.$id)->setField('state',3);
            if($res){
                $this->success('删除成功');
            }else{
                $this->error('出错啦，请检查');
            }
        }else{
            $this->error('请先选择商品');
        }//end fun




    }//end fun






}//end class